//
//  delicateOfCartoonViewController.m
//  ABookOfCartoon
//
//  Created by 美国大狗 on 15-5-15.
//  Copyright (c) 2015年 Power Of Ants. All rights reserved.
//

#import "delicateOfCartoonViewController.h"
#import "JCSeekPageSEEKViewController.h"
#import "JCSeekPageViewController.h"
#import "UIImageView+WebCache.h"
#import "JCReadViewController.h"
#import "JCDataCollectManager.h"

@interface delicateOfCartoonViewController ()<UIScrollViewAccessibilityDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UIImageView *cartoonImage;
@property (nonatomic, retain)UILabel *cartoonName;
@property (nonatomic, retain)UILabel *author;
@property (nonatomic, retain)UILabel *love;
@property (nonatomic, retain)UILabel *over;
@property (nonatomic, retain)UILabel *delicate;
@property (nonatomic, retain)UITableView *lineOfWeb;
@property (nonatomic, retain)UILabel *web;
@property (nonatomic, retain)UIButton *jumpButton;
@property (nonatomic, retain)NSString *XID;

@property (nonatomic, retain)NSMutableDictionary *bookInfo;

@property (nonatomic, retain)UIButton *button1;
//dfgh
@end

@implementation delicateOfCartoonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"漫画详情";

    [self getDataFromWeb23333];
    
//  AFN封装后调用方法:(特方便)
//    NSString *url = [NSString stringWithFormat:@""];
//    
//    [BaseMyAFN getNetData:url responseGETData:^(id data) {
//        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//        dic= [NSMutableDictionary dictionaryWithDictionary:data];
//        
//    }];
    self.lineOfWeb = [[UITableView alloc] initWithFrame:CGRectMake(20, 150 + (self.view.bounds.size.width / 3 + 50) + (self.view.bounds.size.width / 3 + 50) / 4 + 60 + (self.view.bounds.size.width / 3 + 50) + (self.view.bounds.size.width / 3 + 50) / 4, self.view.bounds.size.width - 40, 160) style:UITableViewStylePlain];

    _lineOfWeb.delegate = self;
    _lineOfWeb.dataSource = self;
    _lineOfWeb.backgroundColor = [UIColor whiteColor];
    [(UIScrollView *)[self.view viewWithTag:1001] addSubview:_lineOfWeb];
    [_lineOfWeb release];
    
}

- (void)createScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    scrollView.contentSize = CGSizeMake(0, 1000);
    scrollView.tag = 1001;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    [scrollView release];
    
    self.cartoonImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, self.view.bounds.size.width / 3, self.view.bounds.size.width / 3 + 50)];
    [_cartoonImage sd_setImageWithURL:[NSURL URLWithString:[_dataArr[0] objectForKey:@"BookIconOtherURL"]]];
    [scrollView addSubview:_cartoonImage];
    [_cartoonImage release];
    
    self.XID = [_dataArr[0] objectForKey:@"BookID"];
    
    self.cartoonName = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 3 + 30, 20, self.view.bounds.size.width / 2, ((self.view.bounds.size.width / 3 + 50) / 4))];
//    _cartoonName.text = [_dataArr[0] objectForKey:@"BookName"];
    NSString *cN = [_dataArr[0] objectForKey:@"BookName"];
    NSString *MB = [NSString stringWithFormat:@"书名 %@", cN];
    _cartoonName.text = MB;
    [scrollView addSubview:_cartoonName];
    [_cartoonName release];
    
    self.author = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 3 + 30, 20 + (self.view.bounds.size.width / 3 + 50) / 4  , self.view.bounds.size.width / 2, ((self.view.bounds.size.width / 3 + 50) / 4))];
//    _author.text = [_dataArr[0] objectForKey:@"BookAuthor"];
    _author.text = [NSString stringWithFormat:@"作者 %@", [_dataArr[0] objectForKey:@"BookAuthor"]];
    [scrollView addSubview:_author];
    [_author release];
    
    self.love = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 3 + 30, 20 + (self.view.bounds.size.width / 3 + 50) / 2, self.view.bounds.size.width / 2, ((self.view.bounds.size.width / 3 + 50) / 4))];
//    _author.text = [_dataArr[0] objectForKey:@"BookAuthor"];
    _love.text = [NSString stringWithFormat:@"人气 %@", [_dataArr[0] objectForKey:@"BookClickCount"]];
    [scrollView addSubview:_love];
    [_love release];
    
    self.over = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 3 + 30, 20 + (self.view.bounds.size.width / 3 + 50) - (self.view.bounds.size.width / 3 + 50) / 4, self.view.bounds.size.width / 2, (self.view.bounds.size.width / 3 + 50) / 4)];
    _over.text = [NSString stringWithFormat:@"状态 %@", [_dataArr[0] objectForKey:@"BookStateName"]];
    [scrollView addSubview:_over];
    [_over release];
    
    self.delicate = [[UILabel alloc] initWithFrame:CGRectMake(20, 30 + (self.view.bounds.size.width / 3 + 50) + (self.view.bounds.size.width / 3 + 50) / 4, self.view.bounds.size.width - 40, 300)];
//    _delicate.backgroundColor = [UIColor blackColor];
    _delicate.text = [_dataArr[0] objectForKey:@"BookDescription"];
    _delicate.numberOfLines = 0;
    [_delicate sizeToFit];
    [scrollView addSubview:_delicate];
    [_delicate release];
    
    self.jumpButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _jumpButton.frame = CGRectMake(self.view.frame.size.width / 3, (self.view.bounds.size.width / 3 + 50) + (self.view.bounds.size.width / 3 + 50) / 4 + _delicate.frame.size.height + 70, self.view.frame.size.width / 3, 40);
    [_jumpButton setTitle:@"开始阅读" forState:UIControlStateNormal];
    [_jumpButton addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    [_jumpButton.layer setCornerRadius:5];
    [_jumpButton.layer setBorderWidth:1];
    [scrollView addSubview:_jumpButton];
    
    self.button1 = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3,  (self.view.bounds.size.width / 3 + 50) + (self.view.bounds.size.width / 3 + 50) / 4 + _delicate.frame.size.height + 70 + _jumpButton.frame.size.height, self.view.frame.size.width / 3, 40)];
    //    _button1.backgroundColor = [UIColor redColor];
    [_button1 setTitle:@"收藏" forState:UIControlStateNormal];
    [_button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_button1 addTarget:self action:@selector(but1) forControlEvents:UIControlEventTouchUpInside];
    [_button1.layer setCornerRadius:5];
    [_button1.layer setBorderWidth:1];
    [scrollView addSubview:_button1];
    
    
     scrollView.contentSize = CGSizeMake(0, 180 + (self.view.bounds.size.width / 3 + 50) + (self.view.bounds.size.width / 3 + 50) / 4 + _delicate.frame.size.height);
}

-(void)but1
{
    [_button1 setTitle:@"已收藏" forState:UIControlStateNormal];
    JCDataCollectManager *dbManager = [JCDataCollectManager shareInstance];
    [dbManager openDB];
    [dbManager createTable];
    JCMainPageModel *name = [[JCMainPageModel alloc]init];
    name.BookName = _cartoonName.text;
    //name.BookAuthor = h.BookAuthor;
    [dbManager insertInfowithJCMainPageModel:name];
    [name release];
}

- (void)jump
{
    NSLog(@"跳转!");
    JCReadViewController *sb = [[JCReadViewController alloc] init];
    sb.str7 = _XID;
    [self.navigationController pushViewController:sb animated:YES];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
//    cell.textLabel.text = _dataDic.[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

- (void)getDataFromWeb
{
    //网址 -> string
    NSString *strA = [NSString stringWithFormat:@"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=booksite&bookname=%@&bookid=%@",_BookName, _BookId];
    //有中文?转码!
    NSString *str = [strA stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //string -> url
    NSURL *url = [NSURL URLWithString:str];
    //url -> request
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //建立连接
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.dataArr = [[NSMutableArray array] init];
        _dataArr = result;
        NSLog(@"%@",_dataArr);
        [self createScrollView];
    }];

}
//获取搜索结果的具体信息(主要还是bookId)
- (void)getDataFromWeb23333
{
    
    //网址 -> sting
    NSString *strA = [NSString stringWithFormat:@"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=search&name=%@&type=0", self.sb];
    //有中文?转码!
    NSString *str = [strA stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //string -> url
    NSURL *url = [NSURL URLWithString:str];
    //url -> request
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //建立连接
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.bookInfo = [[NSMutableDictionary dictionary] init];
        //        for (NSMutableDictionary *dic in result) {
        //            _bookInfo = dic;
        //            break;
        //        }
        
        _bookInfo = result[0];
        self.BookId = [_bookInfo objectForKey:@"BookID"];
        self.BookName = [_bookInfo objectForKey:@"BookName"];
        NSLog(@"获取漫画信息完成!");
        [self getDataFromWeb];
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
